<?php
$this->breadcrumbs=array(
	'Pages',
);

$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery.ui');
$cs->registerCoreScript('bbq');
$cs->registerScriptFile(Yii::app()->themeManager->baseUrl.'/Admin/scripts/jquery.ui.nestedSortable.js');
$script = "
var timer = null;
$('#list-container').nestedSortable({
    disableNesting: 'no-nest',
    forcePlaceholderSize: true,
    handle: '.sort-handle',
    items: '.sortable',
    opacity: .6,
    placeholder: 'placeholder',
    tabSize: 25,
    tolerance: 'pointer',
    toleranceElement: '> div',
    'listType' : 'ul',
    start: function(event, ui){
        ui.item.css('backgroundImage', 'none');
    },
    stop: function(event, ui){
        jQuery.updateLine();
    },
    update: function(event, ui){
        jQuery('#list-container').nestedSortable('disable');
        var serialized = jQuery('#list-container').nestedSortable('serialize');
        jQuery('.ordering-updated').remove();
        if (timer) {
            clearTimeout(timer);
            timer = null;
        }
        jQuery.get('".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.page.sort'))."', serialized, function(res){
            jQuery('#list-container').nestedSortable('enable');
            res = eval(res);
            if (res.errors != undefined && res.errors.length <= 0) {
                var message = jQuery('<div></div');
                message.addClass('ordering-updated')
                    .text('The display order of your pages has been updated successfully.');
                jQuery('#list-container').before(message);
                timer = setTimeout(function(){jQuery('.ordering-updated').remove()}, 5000);
            } else {
                var message = 'Error';
                if (jQuery.isArray(res.errors.ErrorCode))
                    message = res.errors.ErrorCode.join('. ');
                alert(message);
            }
        });
    }
});

jQuery.updateLine = function()
{
    jQuery('.page_tree li').css({'background': 'none'});

    jQuery('.page_tree ul > li').css({
        'background': 'url(".Yii::app()->request->hostInfo.Yii::app()->theme->baseUrl."/images/page/page_line_child_single.png) no-repeat left top'
    });

    jQuery('.page_tree > li').has('ul li').css({
        'background': 'url(".Yii::app()->request->hostInfo.Yii::app()->theme->baseUrl."/images/page/page_line_parent_children.png) no-repeat left top'
    });

    jQuery('.page_tree ul li').has('ul li').css({
        'background': 'url(".Yii::app()->request->hostInfo.Yii::app()->theme->baseUrl."/images/page/page_line_child_children.png) no-repeat left top'
    });

    jQuery('.page_tree ul').each(function() {
        jQuery(this).children('li:last').css({
            'background': 'url(".Yii::app()->request->hostInfo.Yii::app()->theme->baseUrl."/images/page/page_line_child_last.jpg) no-repeat left top'
        });
    });

    // sub with sub
    jQuery('.page_tree ul li:last-child').has('ul li').css({
        'background': 'url(".Yii::app()->request->hostInfo.Yii::app()->theme->baseUrl."/images/page/page_line_sublast_children.png) no-repeat left top'
    });
};
jQuery.updateLine();

jQuery('#list-container').delegate('.page_delete', 'click', function(){
    var page = jQuery(this).closest('li');
    var page_id = parseInt(page.attr('id').replace('items-', ''));
    jQuery.get('".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.page.delete'))."', {'ids[]' : page_id}, function(res){
        res = eval(res);
        if (res.errors != undefined && res.errors.length <= 0) {
            page.remove();
        } else {
            var message = 'Error';
            if (jQuery.isArray(res.errors.ErrorCode))
                message = res.errors.ErrorCode.join('. ');
            alert(message);
        }
    });
    return false;
});
jQuery('.page_link_inline').click(function(){
    parent.window.location = jQuery(this).attr('href');
    return false;
});
";
$cs->registerScript(__CLASS__.'#InitNestedSortable', $script);

$script = "
jQuery('.sort-handle .expand, .sort-handle .collapse').click(function(){
    if (jQuery(this).hasClass('expand'))
    {
        jQuery(this).removeClass('expand')
            .closest('.sort-handle').next('ul').show();
    }
    else
    {
        jQuery(this).addClass('expand')
            .closest('.sort-handle').next('ul').hide();
    }
    return false;
});
";
$cs->registerScript(__CLASS__.'#Toggle', $script);
?>

<h1>Pages</h1>
<div class="add-page">
    <?php echo CHtml::link('Add Page', array('/Cms/admin/page/create'), array('title'=>'Add Page', 'class'=>'sub_page button'));?>
</div>
<ul id="list-container" class="page_tree">
    <?php $this->renderNestedPage($models);?>
</ul>
